package com.puboot.module.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.Constants;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.model.*;
import com.puboot.module.admin.service.BizCategoryService;
import com.puboot.module.admin.service.BizContactService;
import com.puboot.module.admin.service.BizTagsService;
import com.puboot.module.admin.vo.base.PageResultVo;
import com.puboot.module.admin.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 联系我们配置
 *
 * @author chen
 * @version V1.0
 * @date 2021年10月18日
 */
@Controller
@RequestMapping("contact")
@AllArgsConstructor
public class BizContactController {

    private final BizContactService bizContactService;


    @PostMapping("list")
    @ResponseBody
    public PageResultVo loadContactList(BizContact bizContact, Integer pageNumber, Integer pageSize) {
        IPage<BizContact> bizContactIPage = bizContactService.pageContactAll(bizContact, pageNumber, pageSize);
        return ResultUtil.table(bizContactIPage.getRecords(), bizContactIPage.getTotal());
    }

    @GetMapping("/add")
    public String add(Model model) {
        BizContact bizContact = bizContactService.findByStatus(Constants.STATE_VAILD);
        model.addAttribute("contact", bizContact);
        return CoreConst.ADMIN_PREFIX + "contact/contact";
    }

    @PostMapping("/add")
    @ResponseBody
    @CacheEvict(value = "contact", allEntries = true)
    public ResponseVo add(BizContact bizContact) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(bizContact.getId() != null){
            BizContact old = bizContactService.getById(bizContact.getId());
            old.setStatus(Constants.STATE_INVAILD);
            old.setUpdateTime(new Date());
            old.setUpdateUserId(user.getUserId());
            bizContactService.updateById(old);
        }
        bizContact.setCreateTime(new Date());
        bizContact.setCreateUserId(user.getUserId());
        bizContact.setStatus(Constants.STATE_VAILD);
        boolean i = bizContactService.save(bizContact);
        if (i) {
            return ResultUtil.success("新增成功");
        } else {
            return ResultUtil.error("新增失败");
        }
    }

    @GetMapping("/edit")
    public String edit(Model model, Integer id) {
        BizContact bizContact = bizContactService.getById(id);
        model.addAttribute("contact", bizContact);
        return CoreConst.ADMIN_PREFIX + "contact/form";
    }

    @PostMapping("/edit")
    @ResponseBody
    @CacheEvict(value = "tag", allEntries = true)
    public ResponseVo edit(BizContact bizContact) {
        bizContact.setUpdateTime(new Date());
        boolean i = bizContactService.updateById(bizContact);
        if (i) {
            return ResultUtil.success("编辑成功");
        } else {
            return ResultUtil.error("编辑失败");
        }
    }

    @PostMapping("/del")
    @ResponseBody
    public ResponseVo delete(Integer id) {
        return deleteBatch(new Integer[]{id});
    }

    @PostMapping("/batch/delete")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids) {
        int i = bizContactService.deleteBatch(ids);
        if (i > 0) {
            return ResultUtil.success("删除成功");
        } else {
            return ResultUtil.error("删除失败");
        }
    }
}
